package com.xxx.modules.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xxx.modules.mapper.UserMapper;
import com.xxx.modules.entity.User;
import com.xxx.modules.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.modules.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Arrays;
import java.util.Objects;

/**
 * user
 *
 * @author 北易航
 * @since 1.0.0 2022-12-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    // 登录
    @Override
    public User login(String userName, String password,Integer userType) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName).eq("password",password).eq("user_type",userType);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public User login(String userName, String oldPassword) {
        return null;
    }

    //修改密码
    @Override
    public User updatePassword(String userName, String oldPassword, String newPassword) {
            QueryWrapper<User> query = new QueryWrapper<>();
            query.eq("user_name", userName);
            query.eq("password", Md5Util.MD5(oldPassword));
        User user = userMapper.selectOne(query);
        if (Objects.isNull(user)) {
            return null;
        }


        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", user.getId());

        User newUser = new User();
        newUser.setPassword(Md5Util.MD5(newPassword));
        userMapper.update(newUser, wrapper);
        return user;
    }

    // 注册
    @Override
    public User register(String userName, String md5Password, Integer userType) {
        // 校验参数
        if (StringUtils.isBlank(userName) || StringUtils.isBlank(md5Password) || userType == null) {
            return null;
        }
        // 根据用户名查询用户是否存在
        User user = userMapper.selectByUserName(userName);
        if (user != null) {
            return null;
        }
        // 注册用户
        user = new User();
        user.setUserName(userName);
        user.setPassword(md5Password);
        user.setUserType(userType);
        userMapper.insert(user);
        return user;
    }


    // 分页查询
    @Override
    public PageInfo<User> selectUserInfo(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (user.getAddress() !=null){
            wrapper.like("address",user.getAddress());
        }
        if (user.getRealName() !=null){
            wrapper.like("real_name",user.getRealName());
        }
        if (user.getPhone() !=null){
            wrapper.like("phone",user.getPhone());
        }
        if (user.getIdCard() !=null){
            wrapper.like("id_card",user.getIdCard());
        }
        if (user.getId() !=null){
            wrapper.eq("id",user.getId());
        }
        if (user.getNumber() !=null){
            wrapper.like("number",user.getNumber());
        }
        if (user.getUserType() !=null){
            wrapper.eq("user_type",user.getUserType());
        }
        if (user.getWorkId() !=null){
            wrapper.eq("work_id",user.getWorkId());
        }
        if (user.getBindingId() !=null){
            wrapper.eq("binding_id",user.getBindingId());
        }
        List<User> data = userMapper.selectList(wrapper);
        for (User user1:data){
            User user2 = userMapper.selectById(user1.getWorkId());
            if (user2 !=null){
                user1.setWorkName(user2.getRealName());
            }else {
                user1.setWorkName("暂无");
            }
        }
        return new PageInfo<>(data);
    }

    @Override
    public void saveUserInfo(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateUserInfo(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void delUserInfo(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void delBatchUserInfo(String idList) {
        List<String> list = Arrays.asList(idList.split(","));
        userMapper.deleteBatchIds(list);
    }


}